import { Users } from './screens/projectList/searchPannel'

const localStorageKey: string = '__auth_provider_token__'
const apiUrl = process.env.REACT_APP_API_URL

export const getToken = () => window.localStorage.getItem(localStorageKey)

export const handleUserResponse = ({ user }: { user: Users }) => {
	window.localStorage.setItem(localStorageKey, user.token || '')
	return user
}

export const register = (data: { username: string; password: string }) => {
	return fetch(`${apiUrl}/register`, {
		method: 'POST',
		headers: {
			'Content-Type': 'application/json'
		},
		body: JSON.stringify(data)
	}).then(async (res: Response) => {
		if (res.ok) {
			return handleUserResponse(await res.json())
		} else {
			return Promise.reject(data)
		}
	})
}

export const login = (data: { username: string; password: string }) => {
	return fetch(`${apiUrl}/login`, {
		method: 'POST',
		headers: {
			'Content-Type': 'application/json'
		},
		body: JSON.stringify(data)
	}).then(async (res: Response) => {
		if (res.ok) {
			return handleUserResponse(await res.json())
		} else {
			return Promise.reject(data)
		}
	})
}

export const logout = async () => window.localStorage.removeItem(localStorageKey)
